package com.dmwork.service.interaction.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dmwork.common.base.result.R;
import com.dmwork.common.base.util.JwtInfo;
import com.dmwork.common.base.util.JwtUtils;
import com.dmwork.service.interaction.entity.WorkCollect;
import com.dmwork.service.interaction.entity.WorkCollectGroup;
import com.dmwork.service.interaction.service.WorkCollectGroupService;
import com.dmwork.service.interaction.service.WorkCollectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;


@Slf4j
@Api(description = "全局收藏管理")
@Transactional
@RestController
@RequestMapping("/front/dm_work/interaction/collect")
public class FrontWorkCollectController {

    @Autowired
    private WorkCollectService workCollectService;
    @Autowired
    private WorkCollectGroupService workCollectGroupService;

    @ApiOperation("获取指定收藏分组的作品id列表")
    @GetMapping("get_work_id_list/{customerId}/{collectGroupId}/{page}/{limit}")
    public R getWorkIdList(
            HttpServletRequest request,
            @ApiParam(value = "用户id", required = true) @PathVariable("customerId") String customerId,
            @ApiParam(value = "收藏分组id", required = true) @PathVariable("collectGroupId") String collectGroupId,
            @ApiParam(value = "页数", required = true) @PathVariable("page") Integer page,
            @ApiParam(value = "每页记录数", required = true) @PathVariable("limit") Integer limit) {

        WorkCollectGroup workCollectGroup = workCollectGroupService.getById(collectGroupId);
        List<String> workIdList = new ArrayList<>();
        try {
            JwtInfo jwtToken = JwtUtils.getCustomerInfoByJwtToken(request);
            //判断：如果token中的用户id和传入的customerId相等，允许获得未公开的收藏分组的作品||收藏分组是公开的
            if ((workCollectGroup.getPublicStatus() == WorkCollectGroup.PRIVATE && jwtToken.getId().equals(customerId))
                    || workCollectGroup.getPublicStatus() == WorkCollectGroup.PUBLIC) {
                WorkCollect workCollect = new WorkCollect();
                workCollect.setCustomerId(customerId);
                workCollect.setCollectGroupId(collectGroupId);
                Page<WorkCollect> workCollectPage = workCollectService.getWorkIdPage(workCollect, page, limit);
                for (WorkCollect collect : workCollectPage.getRecords()) {
                    workIdList.add(collect.getWorkId());
                }
                return R.ok().data("total", workCollectPage.getTotal()).data("workIdList", workIdList);
            }
        } catch (Exception e) {
            //解析异常，可能是token过期或是没有传token，此时若请求的收藏分组是公开的，也可以返回作品列表
            if (workCollectGroup.getPublicStatus() == WorkCollectGroup.PUBLIC) {
                WorkCollect workCollect = new WorkCollect();
                workCollect.setCustomerId(customerId);
                workCollect.setCollectGroupId(collectGroupId);
                Page<WorkCollect> workCollectPage = workCollectService.getWorkIdPage(workCollect, page, limit);
                for (WorkCollect collect : workCollectPage.getRecords()) {
                    workIdList.add(collect.getWorkId());
                }
                return R.ok().data("total", workCollectPage.getTotal()).data("workIdList", workIdList);
            }
        }
        return R.error().message("您无权获得当前分组的作品");
    }

}
